<?php
class Admin_studio extends Studio_Controller{
	function __construct(){
		parent :: __construct();
		$this->load->model("Studio_m");
		$this->_data['sidebar'] = "studio";
	}
	
	function index(){
		$this->studios();	
	}
	
	//--list all studios--//
	function studios(){
		is_admin();
		$this->_data['page_title'] = "studios";
		$per_page = $this->uri->segment(3);
		$off_set = $this->uri->segment(4);
		if($per_page == ""){
			$per_page = 30;
		}
		if($off_set == ""){
			$off_set = "";
		}
		$config['base_url'] = base_url()."index.php/admin_studio/studios/".$per_page."/";
		$config['per_page'] = $per_page;
		$config['uri_segment'] = 4;
		$config['total_rows'] = $this->Studio_m->count_studios();
		if($off_set > 0 && $off_set == $config['total_rows']){
			$off_set = $off_set - $per_page;
		}
		$this->_data['studio_data'] = $this->Studio_m->list_all_studios($per_page, $off_set)->result_array();
		$this->pagination->initialize($config);
    	$this->_data['pagination'] = $this->pagination->create_links();
    	$this->_data['total'] = $config['total_rows'];
    	$this->_data['perpage'] = $per_page;
    	$this->_data['offset'] = $off_set;
    	$this->display_admin('admin/studio/list_studios');
	}
	
	//--add studios--//
	function add_studio(){
		is_admin();
		$this->_data['page_title'] = "Ajoute studio";
		$config = array(
							array("field" => "studio_name", "label" => " nom du studio",
								  "rules"=>"required|xss_clean|trim|callback_check_exist_studio_name"
								  )
		
				       );
		$this->form_validation->set_rules($config);
		if($this->input->post("add_studio")){
			if($this->form_validation->run() === FALSE){
				$this->display_admin("admin/studio/add_studio");		
			}else{
				$data = array("name" => $this->input->post("studio_name"));
				if($this->Studio_m->add_studio($data)){
					$this->session->set_flashdata('error', lang('add') ." ". lang('success'));
    				redirect(site_url('admin_studio'));	
				}else{
					$this->_data['insert_error'] = "Insert failed!";
				}
				
			}
		}
		$this->display_admin("admin/studio/add_studio");
	}
	
	//--check exist studio name--//
	function check_exist_studio_name($studio_name){
		$studios = $this->Studio_m->count_studio_by_name($studio_name);
		if($studios>0){
			$this->form_validation->set_message("check_exist_studio_name", "le nom du studio existe déjà dans le système");
			return FALSE;
		}else{
			return TRUE;
		}
	}
	
	//--edit studio--//
	function edit_studio($studio_id = null){
		is_admin();
		$this->_data['page_title'] = lang('edit_profile')." "."studio";
		if($this->Studio_m->get_studio_details($studio_id) === FALSE){
			redirect("admin_studio");
		}
		$studio_data = $this->Studio_m->get_studio_details($studio_id);
		$this->_data['studios'] = $studio_data;
		$this->_data['studio_name'] = $studio_data->name;
		$this->_data['studio_id'] = $studio_data->id;	
		$config = array(
							array("field" => "studio_name", 
								  "label" => "nom du studio",
								  "rules" => "required|xss_clean|trim"
								 )
		
				       );
		$this->form_validation->set_rules($config);
		
		if($this->input->post('edit_studio')){
			if($this->form_validation->run()){
				$data = array("name"=>$this->input->post("studio_name"));
				if($this->Studio_m->edit_studio($studio_id, $data)){
					$this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
					redirect('admin_studio/studios');	
				}	
			}else{
				$this->display_admin('admin/studio/edit_studio');
			}			
		}else{
			
			$this->display_admin('admin/studio/edit_studio');
		}
		
			
	}
	
	//--delete contact--//
	function delete_studio($id){
		is_admin();
		$delete = $this->Studio_m->delete_studio($id);
		if($delete){
			$this->session->set_flashdata('error', lang('delete')." ".lang('success'));
			redirect('admin_studio/studios');
		}else{
			$this->session->set_flashdata('error', lang('delete')." ".lang('fail'));
			redirect('admin_studio/studios');
		}
	}
	
	//--delete multi contact--//
	function delete_multi_studios(){
		is_admin();
        $list = "";
        $adelete = $this->input->post('delete');
        $N = count($adelete);
        for ($i = 0; $i < $N; $i++) {
            
            if ($adelete[$i] != "")
                $list = $list . ',' . $adelete[$i];
        }
        $list = '(' . substr($list, 1) . ')';

        $query = $this->Studio_m->delete_multi_studios($list);
        if ($query)
            $this->session->set_flashdata('error', lang('delete') ." ". lang('success'));
        else
            $this->session->set_flashdata('error', lang('delete') ." ". lang('fail'));
        redirect('admin_studio/studios');
	}
}